﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmDonDatHangKH : Form
    {
        #region khởi tạo
        bool checkClose = false;
        public static Entities.DonDatHangKH dondathangkh;
        Entities.DonDatHangKH[] hienthi;

        public frmDonDatHangKH(string makhachhang)
        {
            InitializeComponent();
            hienthi = (Entities.DonDatHangKH[])(Common.StaticVariable.InterfaceObject.SelectDonDatHangKH()).Where(p => p.MaKhachHang == makhachhang).ToArray();
            dgvHienThi.DataSource = hienthi;
            fix();
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.DonDatHangKH.MaDonDatHangKH].Visible = true;
            dgvHienThi.Columns[Common.Constants.DonDatHangKH.MaKhachHang].Visible = true;
            dgvHienThi.Columns[Common.Constants.DonDatHangKH.TenKhachHang].Visible = true;

            dgvHienThi.Columns[Common.Constants.DonDatHangKH.MaDonDatHangKH].HeaderText = "Mã Hóa Đơn";
            dgvHienThi.Columns[Common.Constants.DonDatHangKH.TenKhachHang].HeaderText = "Tên Khách Hàng";
            dgvHienThi.Columns[Common.Constants.DonDatHangKH.MaKhachHang].HeaderText = "Mã Khách Hàng";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Tìm Kiếm
        public void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.DonDatHangKH[])hienthi.Where(p => p.TenKhachHang.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
        }
        #endregion

        #region Chọn
        public void Chon()
        {
            if(i<0)
            {
                return;
            }
            if (dgvHienThi.RowCount == 0)
            {
                return;
            }
            dondathangkh = ((Entities.DonDatHangKH[])dgvHienThi.DataSource)[i];
            checkClose = true;
            this.Close();
        }
        #endregion

        #region thoát
        public void Thoat()
        {
            dondathangkh = null;
            this.Close();
        }
        #endregion

        #region Event
        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            btnChon.Click += new EventHandler(btnChon_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            btnTim.Click += new EventHandler(btnTim_Click);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            this.FormClosing += new FormClosingEventHandler(frmKhachHang_FormClosing);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Chon();
            }
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void frmKhachHang_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Chon();
        }
        int i;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnChon_Click(object sender, EventArgs e)
        {
            Chon();
        }
        #endregion

    }
}
